﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using Ferpect.ComponentModel.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Ferpect.ComponentModel.Design
{
    public class VideoSpriteDesigner : SpriteDesigner, IDrawable
    {
        private ContentManager content;
        private Texture2D mediaTexture;
        private SpriteBatch spriteBatch;

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            IServiceProvider serviceProvider = (IServiceProvider)this.GetService(typeof(IDesignerHost));
            this.content = new EmbeddedContentManager(serviceProvider, this.GetType().Assembly, this.GetType().Namespace + ".EmbeddedContent");
            this.mediaTexture = this.content.Load<Texture2D>("MediaBox");
            IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)this.GetService(typeof(IGraphicsDeviceService));
            this.spriteBatch = new SpriteBatch(graphicsService.GraphicsDevice);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.content.Dispose();
                this.mediaTexture = null;
            }
            base.Dispose(disposing);
        }

        public override bool SpriteCanMove
        {
            get
            {
                VideoSprite videoSprite = this.Component as VideoSprite;
                if (videoSprite != null)
                {
                    return !videoSprite.FullScreen;
                }
                Debug.Fail("The ImageSpriteDesigner is being used with a sprite that it doesn't recognize.");
                return true;
            }
        }

        #region IDrawable Members

        public void Draw(GameTime gameTime)
        {
            VideoSprite sprite = (VideoSprite)this.Component;
            if (sprite.FullScreen)
            {
                IViewportService viewportService = (IViewportService)this.GetService(typeof(IViewportService));
                Rectangle viewport = sprite.UseTitleSafeArea ? viewportService.TitleSafeArea : viewportService.ViewportArea;
                this.spriteBatch.Begin();
                try
                {
                    this.spriteBatch.Draw(this.mediaTexture, viewport, null, sprite.Color, 0, Vector2.Zero, sprite.Effects, sprite.LayerDepth);
                }
                finally
                {
                    this.spriteBatch.End();
                }
            }
            else
            {
                float scale = 1f;
                Vector2 scale2 = new Vector2();
                if (sprite.Video != null)
                {
                    float scaleX = (float)sprite.Video.Width / (float)this.mediaTexture.Width;
                    float scaleY = (float)sprite.Video.Height / (float)this.mediaTexture.Height;
                    scale = MathHelper.Min(scaleX, scaleY);
                    scale2.X = scaleX;
                    scale2.Y = scaleY;
                }
                this.spriteBatch.Begin();
                try
                {
                    this.spriteBatch.Draw(this.mediaTexture, sprite.Position, null, sprite.Color, sprite.Rotation, new Vector2(sprite.Origin.X/scale2.X, sprite.Origin.Y/scale2.Y), scale2 * sprite.Scale, sprite.Effects, sprite.LayerDepth);
                }
                finally
                {
                    this.spriteBatch.End();
                }
            }
        }

        public int DrawOrder
        {
            get { return 0; }
        }

        public event EventHandler<EventArgs> DrawOrderChanged { add { } remove { } }

        public bool Visible
        {
            get { return true; }
        }

        public event EventHandler<EventArgs> VisibleChanged { add { } remove { } }

        #endregion
    }
}
